<?php 

namespace Admin\Controller;

/**
 * 幻灯片类
 */
class SlideController extends CommonController{
	public function index(){
		$this -> slide = M('slide')-> order('sort') -> select();
		$this->display();
	}

	public function add(){
		if (IS_POST) {
            $_POST['time']=time();
			if ( M('slide') -> add($_POST)) {
				$this -> success('添加成功！', U(MODULE_NAME . '/Slide/index'));
			} else {
				$this -> error('添加失败！');
			}
		} else {

			$this -> display();
		}
	}
	
	public function edit(){
		if (IS_POST) {
			
			$id = I('id', 0, 'intval');
			// 获取要修改的栏目ID
			$name = $_POST['name'];
			$sort = $_POST['sort'];
			$url = $_POST['url'];
			$imagefile = $_POST['imagefile'];
			$where = array('id' => $id);
			$db = M('slide') -> where($where) -> save(array('name' => $name, 'sort' => $sort, 'url' => $url, 'imagefile' => $imagefile));
			if ($db) {
				$this -> success('修改成功！', U(MODULE_NAME . '/Slide/index'));
			} else {
				$this -> error('修改失败！');
			}
		} else {
			$id = I('id', 0, 'intval');
			// 获取要修改的ID
			$slide = M('slide') -> where(array('id' => $id)) -> find();
			$this -> slide = $slide;
			$this -> display();
		}
	}
    //异步排序
    public function change(){
        //更新当前栏目的排序
        $res = M('slide')->where(array('id'=>$_POST['id']))->save(array('sort'=>$_POST['sort']));
        if($res){
            $data =['status'=>1,'info'=>'更新成功！',];
        }else{
            $data = ['status'=>0,'info'=>'更新失败，请稍后再试！',];
        }
        $this->ajaxReturn($data);
    }




    // 删除
    public function delete() {
        $id = I('id', 0, 'intval');
        if (M('slide')->where(array('id' => $id))->delete()) {
            $data =['status'=>1,'info'=>'删除成功！',];
        }else{
            $data = ['status'=>0,'info'=>'删除失败，请稍后再试！',];
        }
        $this->ajaxReturn($data);
    }
}
